package nc.vo.er.settle;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import nc.bs.er.settle.ISettlementInfoFetcherForErm;
import nc.bs.framework.common.NCLocator;
import nc.cmp.utils.NetPayHelper;
import nc.itf.bd.psn.psndoc.IPsndocQueryService;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ArrayProcessor;
import nc.md.persist.framework.MDPersistenceService;
import nc.pubitf.uapbd.ICustomerPubService;
import nc.pubitf.uapbd.ISupplierPubService;
import nc.vo.bd.cust.CustomerVO;
import nc.vo.bd.freecustom.FreeCustomVO;
import nc.vo.bd.supplier.SupplierVO;
import nc.vo.cmp.netpay.NetPayHelperVO;
import nc.vo.cmp.settlement.SettlementBodyVO;
import nc.vo.cmp.settlement.SettleEnumCollection.Direction;
import nc.vo.ep.bx.BXBusItemVO;
import nc.vo.ep.bx.JKBXHeaderVO;
import nc.vo.ep.bx.JKBXVO;
import nc.vo.jcom.lang.StringUtil;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nccloud.commons.lang.StringUtils;

public class ErmSettlementInfo implements ISettlementInfoFetcherForErm {
	public SettlementBodyVO[] getSettleMetaInfo(JKBXVO bxvo) throws BusinessException {
		BXBusItemVO[] childrenVO = bxvo.getChildrenVO();
		JKBXHeaderVO head = bxvo.getParentVO();
		String oppAccount;
		if (childrenVO == null || childrenVO.length == 0) {
			BXBusItemVO bxBusItemVO = new BXBusItemVO();
			String[] money_fields = BXBusItemVO.MONEY_FIELDS;
			if (head.getPk_jkbx() != null) {
				bxBusItemVO.setPrimaryKey(head.getPk_jkbx());
				bxBusItemVO.setPk_jkbx(head.getPk_jkbx());
			} else {
				bxBusItemVO.setPrimaryKey("ER_SET_HTEMPK_");
				bxBusItemVO.setPk_jkbx("ER_SET_HTEMPK_");
			}

			String[] var6 = money_fields;
			int var7 = money_fields.length;

			for (int var8 = 0; var8 < var7; ++var8) {
				oppAccount = var6[var8];
				bxBusItemVO.setAttributeValue(oppAccount, head.getAttributeValue(oppAccount));
			}

			bxBusItemVO.setJkbxr(head.getJkbxr());
			bxBusItemVO.setSzxmid(head.getSzxmid());
			bxBusItemVO.setJobid(head.getJobid());
			bxBusItemVO.setCashproj(head.getCashproj());
			childrenVO = new BXBusItemVO[]{bxBusItemVO};
		}

		List<SettlementBodyVO> metavoList = new ArrayList();
		int traderType = 0;
		String traderName = null;
		String pk_trader = null;
		String pk_oppAccount = null;
		oppAccount = null;
		String oppAccountName = null;
		Integer accountType = null;
		String pk_oopBank = null;
		String bankDocName = null;
		if (head.getPaytarget() == null || head.getPaytarget() != 1 && head.getPaytarget() != 2) {
			traderType = 3;
			pk_trader = head.getReceiver();
			if (pk_trader != null) {
				traderName = ((IPsndocQueryService) NCLocator.getInstance().lookup(IPsndocQueryService.class))
						.queryPsndocVOsByCondition("pk_psndoc='" + pk_trader + "'")[0].getName();
			}

			pk_oppAccount = head.getSkyhzh();
		} else if (head.getFreecust() != null) {
			FreeCustomVO freeCustVO = (FreeCustomVO) MDPersistenceService.lookupPersistenceQueryService()
					.queryBillOfVOByPK(FreeCustomVO.class, head.getFreecust(), false);
			if (freeCustVO != null) {
				traderType = 4;
				pk_trader = freeCustVO.getPk_freecustom();
				traderName = freeCustVO.getName();
				oppAccountName = freeCustVO.getName();
				oppAccount = freeCustVO.getBankaccount();
				pk_oopBank = freeCustVO.getBank();
				accountType = freeCustVO.getAccountproperty();
				if (StringUtils.isEmpty(pk_oopBank)) {
					throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2011005_expenseaccount_0",
							"02011005_expenseaccount-0054"));
				}

				if (StringUtils.isEmpty(oppAccount)) {
					throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("2011005_expenseaccount_0",
							"02011005_expenseaccount-0055"));
				}
			}
		} else if (head.getPaytarget() == 1) {
			traderType = 1;
			pk_trader = head.getHbbm();
			pk_oppAccount = head.getCustaccount();
			if (head.getHbbm() != null) {
				SupplierVO[] hbbmname = ((ISupplierPubService) NCLocator.getInstance()
						.lookup(ISupplierPubService.class)).getSupplierVO(new String[]{head.getHbbm()},
								new String[]{"name"});
				traderName = hbbmname[0].getName();
			}
		} else if (head.getPaytarget() == 2) {
			traderType = 0;
			pk_trader = head.getCustomer();
			pk_oppAccount = head.getCustaccount();
			if (head.getCustomer() != null) {
				CustomerVO[] customervos = ((ICustomerPubService) NCLocator.getInstance()
						.lookup(ICustomerPubService.class)).getCustomerVO(new String[]{head.getCustomer()},
								new String[]{"name"});
				traderName = customervos[0].getName();
			}
		}

		if (pk_oopBank != null) {
			Object[] result = (Object[]) ((Object[]) ((IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class))
					.executeQuery("select name from bd_bankdoc where pk_bankdoc='" + pk_oopBank + "'",
							new ArrayProcessor()));
			if (result != null && result.length != 0 && result[0] != null) {
				bankDocName = (String) result[0];
			}
		}

		if (pk_oppAccount != null) {
			NetPayHelperVO opphelperVO = NetPayHelper.instance.getNetPayVO(pk_oppAccount);
			if (opphelperVO != null) {
				oppAccount = opphelperVO.getAccount();
				oppAccountName = opphelperVO.getAccname();
				if (bankDocName == null) {
					bankDocName = opphelperVO.getBankdocname();
				}
			}
		}

		UFDouble ufZero = UFDouble.ZERO_DBL;
		String dztz_batch = head.getDztz_batch();
		BXBusItemVO[] var16 = childrenVO;
		int var17 = childrenVO.length;

		for (int var18 = 0; var18 < var17; ++var18) {
			BXBusItemVO item = var16[var18];
			if (item.getZfybje() != null && !item.getZfybje().equals(ufZero)
					|| item.getHkybje() != null && !item.getHkybje().equals(ufZero)) {
				SettlementBodyVO metavo = new SettlementBodyVO();
				metavo.setTranserial(dztz_batch);
				Integer direction = item.getZfybje() != null && !item.getZfybje().equals(ufZero)
						? Direction.PAY.VALUE
						: Direction.REC.VALUE;
				metavo.setBillcode(head.getDjbh());
				metavo.setBilldate(head.getDjrq());
				metavo.setDirection(direction);
				metavo.setMemo(head.getZy());
				metavo.setPay(item.getZfybje());
				metavo.setPaylocal(item.getZfbbje());
				metavo.setReceive(item.getHkybje());
				metavo.setReceivelocal(item.getHkbbje());
				metavo.setSystemcode("erm");
				metavo.setPk_billdetail(item.getPrimaryKey());
				metavo.setPk_billtype(head.getDjlxbm());
				metavo.setPk_bill(head.getPk_jkbx());
				metavo.setPk_org(head.getPk_payorg());
				metavo.setPk_org_v(head.getPk_payorg_v());
				metavo.setPk_group(head.getPk_group());
				metavo.setGrouppaylocal(item.getGroupzfbbje() == null ? ufZero : item.getGroupzfbbje());
				metavo.setGlobalpaylocal(item.getGlobalzfbbje() == null ? ufZero : item.getGlobalzfbbje());
				metavo.setGroupreceivelocal(item.getGrouphkbbje() == null ? ufZero : item.getGrouphkbbje());
				metavo.setGlobalreceivelocal(item.getGlobalhkbbje() == null ? ufZero : item.getGlobalhkbbje());
				metavo.setPk_rescenter(head.getFydeptid());
				metavo.setNotenumber(head.getPjh());
				metavo.setPk_notetype(head.getChecktype());
				if ("iweb".equals(head.getSrcsystem())) {
					metavo.setPk_currtype(item.getBzbm());
					metavo.setLocalrate(item.getBbhl());
					metavo.setGrouprate(item.getGroupbbhl() == null ? ufZero : item.getGroupbbhl());
					metavo.setGlobalrate(item.getGlobalbbhl() == null ? ufZero : item.getGlobalbbhl());
				} else {
					metavo.setPk_currtype(head.getBzbm());
					metavo.setLocalrate(head.getBbhl());
					metavo.setGrouprate(head.getGroupbbhl() == null ? ufZero : head.getGroupbbhl());
					metavo.setGlobalrate(head.getGlobalbbhl() == null ? ufZero : head.getGlobalbbhl());
				}

				metavo.setTradertype(Integer.valueOf(traderType));
				metavo.setPk_trader(pk_trader);
				metavo.setTradername(traderName);
				metavo.setPk_oppaccount(pk_oppAccount);
				metavo.setOppaccount(oppAccount);
				metavo.setOppaccname(oppAccountName);
				metavo.setPk_oppbank(pk_oopBank);
				metavo.setOppbank(bankDocName);
				metavo.setAccounttype(accountType);
				metavo.setTranstype(2);
				metavo.setPk_costsubj(item.getSzxmid());
				metavo.setPk_plansubj(head.getCashproj());
				metavo.setPk_balatype(head.getJsfs());
				metavo.setPk_psndoc(head.getJkbxr());
				metavo.setPk_deptdoc(head.getDeptid());
				metavo.setPk_account(head.getFkyhzh());
				metavo.setPk_cashaccount(head.getPk_cashaccount());
				metavo.setPk_job(item.getJobid());
				metavo.setPk_cashflow(head.getCashitem());
				metavo.setPk_busitype(head.getBusitype());
				if (head.getPaytarget() != null) {
					if (head.getPaytarget() == 1) {
						metavo.setPk_cubasdoc(head.getHbbm());
					} else if (head.getPaytarget() == 2) {
						metavo.setPk_cubasdoc(head.getCustomer());
					}
				}

				metavo.setPk_jobphase(item.getProjecttask());
				metavo.setPk_invbasdoc((String) null);
				metavo.setPk_invcl((String) null);
				metavo.setFundsflag(metavo.getDirection());
				metavoList.add(metavo);
			}
		}

		return (SettlementBodyVO[]) metavoList.toArray(new SettlementBodyVO[0]);
	}

	private SettlementBodyVO[] getSettleMutiMetaInfo(JKBXVO bxvo) throws BusinessException {
		BXBusItemVO[] childrenVO = bxvo.getChildrenVO();
		JKBXHeaderVO head = bxvo.getParentVO();
		List<SettlementBodyVO> metavoList = new ArrayList();
		int var9;
		if (childrenVO == null || childrenVO.length == 0) {
			BXBusItemVO bxBusItemVO = new BXBusItemVO();
			String[] money_fields = BXBusItemVO.MONEY_FIELDS;
			if (head.getPk_jkbx() != null) {
				bxBusItemVO.setPrimaryKey(head.getPk_jkbx());
				bxBusItemVO.setPk_jkbx(head.getPk_jkbx());
			} else {
				bxBusItemVO.setPrimaryKey("ER_SET_HTEMPK_");
				bxBusItemVO.setPk_jkbx("ER_SET_HTEMPK_");
			}

			String[] var7 = money_fields;
			int var8 = money_fields.length;

			for (var9 = 0; var9 < var8; ++var9) {
				String money = var7[var9];
				bxBusItemVO.setAttributeValue(money, head.getAttributeValue(money));
			}

			bxBusItemVO.setPaytarget(head.getPaytarget());
			bxBusItemVO.setReceiver(head.getReceiver());
			bxBusItemVO.setSkyhzh(head.getSkyhzh());
			bxBusItemVO.setHbbm(head.getHbbm());
			bxBusItemVO.setCustomer(head.getCustomer());
			bxBusItemVO.setCustaccount(head.getCustaccount());
			bxBusItemVO.setFreecust(head.getFreecust());
			bxBusItemVO.setSzxmid(head.getSzxmid());
			bxBusItemVO.setJobid(head.getJobid());
			bxBusItemVO.setCashproj(head.getCashproj());
			childrenVO = new BXBusItemVO[]{bxBusItemVO};
		}

		Map<String, Object> cache = new LinkedHashMap();
		UFDouble ufZero = UFDouble.ZERO_DBL;
		Boolean sameBZ = this.isSameBZ(head, childrenVO);
		BXBusItemVO[] var27 = childrenVO;
		var9 = childrenVO.length;

		for (int var28 = 0; var28 < var9; ++var28) {
			BXBusItemVO item = var27[var28];
			if (item.getZfybje() != null && !item.getZfybje().equals(ufZero)
					|| item.getHkybje() != null && !item.getHkybje().equals(ufZero)) {
				SettlementBodyVO metavo = new SettlementBodyVO();
				int traderType = 0;
				String traderName = null;
				String pk_trader = null;
				String bankDocName = null;
				String pk_oopBank = null;
				String pk_oppAccount = null;
				String oppAccount = null;
				String oppAccountName = null;
				Integer accountType = null;
				Integer paytarget = item.getPaytarget();
				if (paytarget == null) {
					paytarget = head.getPaytarget();
				}

				FreeCustomVO freeCustVO;
				if (paytarget != null && paytarget != 0) {
					if (paytarget != 1 && paytarget != 2) {
						if (paytarget == 3) {
							traderType = 3;
							oppAccount = item.getDefitem38();
							oppAccountName = item.getDefitem37();
							bankDocName = item.getDefitem36();
						}
					} else if (item.getFreecust() != null) {
						freeCustVO = null;
						if (cache.get(item.getFreecust()) != null) {
							freeCustVO = (FreeCustomVO) cache.get(item.getFreecust());
						} else {
							freeCustVO = (FreeCustomVO) MDPersistenceService.lookupPersistenceQueryService()
									.queryBillOfVOByPK(FreeCustomVO.class, item.getFreecust(), false);
							cache.put(item.getFreecust(), freeCustVO);
						}

						if (freeCustVO != null) {
							traderType = 4;
							pk_trader = freeCustVO.getPk_freecustom();
							traderName = freeCustVO.getName();
							oppAccountName = freeCustVO.getName();
							oppAccount = freeCustVO.getBankaccount();
							pk_oopBank = freeCustVO.getBank();
							accountType = freeCustVO.getAccountproperty();
							if (StringUtils.isEmpty(pk_oopBank)) {
								throw new BusinessException(NCLangRes4VoTransl.getNCLangRes()
										.getStrByID("2011005_expenseaccount_0", "02011005_expenseaccount-0054"));
							}

							if (StringUtils.isEmpty(oppAccount)) {
								throw new BusinessException(NCLangRes4VoTransl.getNCLangRes()
										.getStrByID("2011005_expenseaccount_0", "02011005_expenseaccount-0055"));
							}
						}
					} else if (paytarget == 1) {
						traderType = 1;
						metavo.setPk_cubasdoc(item.getHbbm());
						pk_trader = item.getHbbm();
						pk_oppAccount = item.getCustaccount();
						if (cache.get(pk_trader) != null) {
							traderName = (String) cache.get(pk_trader);
						} else if (item.getHbbm() != null) {
							SupplierVO[] hbbmname = ((ISupplierPubService) NCLocator.getInstance()
									.lookup(ISupplierPubService.class)).getSupplierVO(new String[]{item.getHbbm()},
											new String[]{"name"});
							traderName = hbbmname[0].getName();
							cache.put(pk_trader, traderName);
						}
					} else if (paytarget == 2) {
						traderType = 0;
						pk_trader = item.getCustomer();
						metavo.setPk_cubasdoc(item.getCustomer());
						pk_oppAccount = item.getCustaccount();
						if (cache.get(pk_trader) != null) {
							traderName = (String) cache.get(pk_trader);
						} else if (item.getCustomer() != null) {
							CustomerVO[] customervos = ((ICustomerPubService) NCLocator.getInstance()
									.lookup(ICustomerPubService.class)).getCustomerVO(new String[]{item.getCustomer()},
											new String[]{"name"});
							traderName = customervos[0].getName();
							cache.put(pk_trader, traderName);
						}
					}
				} else {
					traderType = 3;
					pk_trader = item.getReceiver();
					pk_oppAccount = item.getSkyhzh();
					if (pk_trader != null) {
						if (cache.get(pk_trader) != null) {
							traderName = (String) cache.get(pk_trader);
						} else {
							traderName = ((IPsndocQueryService) NCLocator.getInstance()
									.lookup(IPsndocQueryService.class))
											.queryPsndocVOsByCondition("pk_psndoc='" + pk_trader + "'")[0].getName();
							cache.put(pk_trader, traderName);
						}
					}
				}

				if (paytarget.compareTo(3) != 0) {
					if (pk_oopBank != null) {
						if (cache.get(pk_oopBank) != null) {
							bankDocName = (String) cache.get(pk_oopBank);
						} else {
							Object[] result = (Object[]) ((Object[]) ((IUAPQueryBS) NCLocator.getInstance()
									.lookup(IUAPQueryBS.class)).executeQuery(
											"select name from bd_bankdoc where pk_bankdoc='" + pk_oopBank + "'",
											new ArrayProcessor()));
							if (result != null && result.length != 0 && result[0] != null) {
								bankDocName = (String) result[0];
								cache.put(pk_oopBank, bankDocName);
							}
						}
					}

					if (pk_oppAccount != null && pk_oopBank == null) {
						freeCustVO = null;
						NetPayHelperVO opphelperVO;
						if (cache.get(pk_oppAccount) != null) {
							opphelperVO = (NetPayHelperVO) cache.get(pk_oppAccount);
						} else {
							opphelperVO = NetPayHelper.instance.getNetPayVO(pk_oppAccount);
							cache.put(pk_oppAccount, opphelperVO);
						}

						if (opphelperVO != null) {
							oppAccount = opphelperVO.getAccount();
							oppAccountName = opphelperVO.getAccname();
							bankDocName = opphelperVO.getBankdocname();
						}
					}
				}

				metavo.setTradertype(Integer.valueOf(traderType));
				metavo.setPk_trader(pk_trader);
				metavo.setTradername(traderName);
				metavo.setOppaccount(oppAccount);
				metavo.setOppaccname(oppAccountName);
				metavo.setOppbank(bankDocName);
				metavo.setAccounttype(accountType);
				if (paytarget.compareTo(3) != 0) {
					if (pk_oopBank != null) {
						metavo.setPk_oppbank(pk_oopBank);
					} else {
						metavo.setPk_oppaccount(pk_oppAccount);
					}
				}

				metavo.setBillcode(head.getDjbh());
				metavo.setBilldate(head.getDjrq());
				metavo.setMemo(head.getZy());
				metavo.setPk_billtype(head.getDjlxbm());
				metavo.setPk_bill(head.getPk_jkbx());
				metavo.setPk_org(head.getPk_payorg());
				metavo.setPk_org_v(head.getPk_payorg_v());
				metavo.setPk_group(head.getPk_group());
				metavo.setPk_rescenter(head.getFydeptid());
				metavo.setNotenumber(head.getPjh());
				metavo.setPk_notetype(head.getChecktype());
				metavo.setPk_plansubj(head.getCashproj());
				if (!StringUtils.isEmpty(item.getJsfs())) {
					metavo.setPk_balatype(item.getJsfs());
				} else {
					metavo.setPk_balatype(head.getJsfs());
				}

				metavo.setPk_cashaccount(head.getPk_cashaccount());
				metavo.setPk_busitype(head.getBusitype());
				metavo.setPk_psndoc(head.getJkbxr());
				metavo.setPk_cashflow(head.getCashitem());
				metavo.setPk_deptdoc(head.getDeptid());
				if ("iweb".equals(head.getSrcsystem())) {
					if (sameBZ) {
						metavo.setPk_account(head.getFkyhzh());
					}

					metavo.setPk_currtype(item.getBzbm());
					metavo.setLocalrate(item.getBbhl());
					metavo.setGrouprate(item.getGroupbbhl() == null ? ufZero : item.getGroupbbhl());
					metavo.setGlobalrate(item.getGlobalbbhl() == null ? ufZero : item.getGlobalbbhl());
				} else {
					metavo.setPk_account(head.getFkyhzh());
					metavo.setPk_currtype(head.getBzbm());
					metavo.setLocalrate(head.getBbhl());
					metavo.setGrouprate(head.getGroupbbhl() == null ? ufZero : head.getGroupbbhl());
					metavo.setGlobalrate(head.getGlobalbbhl() == null ? ufZero : head.getGlobalbbhl());
				}

				Integer direction = item.getZfybje() != null && !item.getZfybje().equals(ufZero)
						? Direction.PAY.VALUE
						: Direction.REC.VALUE;
				metavo.setDirection(direction);
				metavo.setPay(item.getZfybje());
				metavo.setPaylocal(item.getZfbbje());
				metavo.setReceive(item.getHkybje());
				metavo.setReceivelocal(item.getHkbbje());
				metavo.setSystemcode("erm");
				metavo.setPk_billdetail(item.getPrimaryKey());
				metavo.setGrouppaylocal(item.getGroupzfbbje() == null ? ufZero : item.getGroupzfbbje());
				metavo.setGlobalpaylocal(item.getGlobalzfbbje() == null ? ufZero : item.getGlobalzfbbje());
				metavo.setGroupreceivelocal(item.getGrouphkbbje() == null ? ufZero : item.getGrouphkbbje());
				metavo.setGlobalreceivelocal(item.getGlobalhkbbje() == null ? ufZero : item.getGlobalhkbbje());
				metavo.setTranstype(2);
				metavo.setPk_costsubj(item.getSzxmid());
				metavo.setPk_job(item.getJobid());
				metavo.setPk_jobphase(item.getProjecttask());
				metavo.setPk_invbasdoc((String) null);
				metavo.setPk_invcl((String) null);
				metavo.setFundsflag(metavo.getDirection());
				metavoList.add(metavo);
			}
		}

		return (SettlementBodyVO[]) metavoList.toArray(new SettlementBodyVO[0]);
	}

	public Boolean isSameBZ(JKBXHeaderVO head, BXBusItemVO[] childrenVO) {
		boolean b = true;
		String bzbmHead = head.getBzbm();
		BXBusItemVO[] var5 = childrenVO;
		int var6 = childrenVO.length;

		for (int var7 = 0; var7 < var6; ++var7) {
			BXBusItemVO item = var5[var7];
			String bzbm = item.getBzbm();
			if (!StringUtil.isEmpty(bzbm) && !bzbmHead.equals(bzbm)) {
				b = false;
				break;
			}
		}

		return b;
	}

	public SettlementBodyVO[] getSettleMetaInfo(Object arg0) throws BusinessException {
		JKBXVO bxvo = (JKBXVO) arg0;
		return !"jk".equals(bxvo.getParentVO().getDjdl()) && !"2647".equals(bxvo.getParentVO().getDjlxbm())
				? this.getSettleMutiMetaInfo(bxvo)
				: this.getSettleMetaInfo(bxvo);
	}
}